package com.borrower.service;

import com.borrower.dao.BorrowerDao;
import com.borrower.dao.BorrowerDaoImpl;

import com.borrower.dto.BorrowInfoBean;
import com.pojo.BookDamageBean;
import com.pojo.BorrowerBean;
import com.pojo.QueryBookBean;
import com.pojo.UserSendMsgBean;
import com.utils.DBUtil;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class BorrowerServiceImpl implements BorrowerService {

    private BorrowerDao borDao= new BorrowerDaoImpl();;

    public BorrowerServiceImpl() {

    }

    @Override
    public boolean Regis(BorrowerBean bor) {
        boolean bool = false;
        Connection conn = null;
        try {
            conn = DBUtil.getConn();
            conn.setAutoCommit(false);
            String sql = "insert into borrower(username, borpwd, email, idcard, phone) values(?,?,?,?,?)";
            bool = borDao.Regis(sql, conn, bor);
            if (bool) {
                conn.commit();
            } else {
                conn.rollback();
            }
        } catch (SQLException e){
            e.printStackTrace();
            try {
                if(conn != null)
                    conn.rollback();
            } catch (SQLException throwable) {
                throwable.printStackTrace();
            }

        } finally {
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        return bool;
    }

    //判断注册时用户名是否存在
    @Override
    public boolean RegisName(BorrowerBean bor) {
        boolean bool = false;
        Connection conn = null;
        try {
            conn = DBUtil.getConn();
            conn.setAutoCommit(false);
            String sql = "SELECT * FROM borrower WHERE username=?";
            bool = borDao.RegisName(sql, conn, bor);
            if (bool) {
                conn.commit();
            } else {
                conn.rollback();
            }
        } catch (SQLException e){
            e.printStackTrace();
            try {
                if(conn != null)
                    conn.rollback();
            } catch (SQLException throwable) {
                throwable.printStackTrace();
            }

        } finally {
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        return bool;
    }

    /**
     * 用户登录
     * @param bor
     * @return
     */
    @Override
    public boolean Login(BorrowerBean bor) {
        boolean bool = false;
        Connection conn = null;
        try {
            conn = DBUtil.getConn();
            conn.setAutoCommit(false);
            String sql = "SELECT *FROM borrower where username=? and borpwd=? and iscancel!='2'";
            bool = borDao.Login(sql, conn, bor);
            if (bool) {
                conn.commit();
            } else {
                conn.rollback();
            }
        } catch (SQLException e){
            e.printStackTrace();
            try {
                if(conn != null)
                    conn.rollback();
            } catch (SQLException throwable) {
                throwable.printStackTrace();
            }

        } finally {
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        return bool;
    }

    @Override
    public List<BorrowerBean> getBorInfoByIdCard(BorrowerBean bb) {
        List<BorrowerBean> list = null;
        Connection conn = null;
        try {
            conn = DBUtil.getConn();
            conn.setAutoCommit(false);
            String sql = "select * from borrower where idcard = ?";
            list = borDao.getBorInfoByIdCard(bb, conn, sql);
            conn.commit();
        } catch (SQLException e) {
            e.printStackTrace();
            try {
                conn.rollback();
            } catch (SQLException throwable) {
                throwable.printStackTrace();
            }
        } finally {
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        return list;
    }

    @Override
    public List<QueryBookBean> queryBookInfoByName(String bookName) {
        List<QueryBookBean> list = null;
        Connection conn = null;
        try {
            conn = DBUtil.getConn();
            conn.setAutoCommit(false);
            String sql = "SELECT username,bookname,bordate,repaydate,press,bi.price from borrower, books bs,borinfo bi where bs.bookid=bi.bookid  and bookName=?";
            list = borDao.getBookInfoByName(bookName, conn, sql);
            conn.commit();
        } catch (SQLException e) {
            e.printStackTrace();
            try {
                conn.rollback();
            } catch (SQLException throwable) {
                throwable.printStackTrace();
            }
        } finally {
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        return list;
    }



    //显示全部借书信息列表
    @Override
    public List<BorrowInfoBean> BorrowInfoService(String loginName) {

        Connection conn = null;
        List<BorrowInfoBean> a = null;
        try {
            conn = DBUtil.getConn();
            conn.setAutoCommit(false);
            String sql = "SELECT x.bookid,borrower.username,x.bookname,x.bordate,x.repaydate,x.price,x.state from(SELECT books.bookid bookid, borid,bookname,bordate,repaydate,borinfo.price price,borinfo.state state FROM borinfo,books WHERE borinfo.bookid=books.bookid)x,borrower WHERE borrower.userid=x.borid and borrower.username=? ";

            a= borDao.BorrowInfo(sql,conn,loginName);
            conn.commit();

        } catch (SQLException e) {
            e.printStackTrace();
            try {
                conn.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }finally {
            if (conn !=null){
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        return a;
    }

    public  boolean SendMsg(UserSendMsgBean userSendMsgBean){
        boolean bool = false;
        Connection conn = null;
        try {
            conn = DBUtil.getConn();
            conn.setAutoCommit(false);
            String sql = "insert into usersendmsg values(null,?,?,?)";
            bool = borDao.SendMsgDao(sql, conn, userSendMsgBean);
            if (bool) {
                conn.commit();
            } else {
                conn.rollback();
            }
        } catch (SQLException e){
            e.printStackTrace();
            try {
                if(conn != null)
                    conn.rollback();
            } catch (SQLException throwable) {
                throwable.printStackTrace();
            }

        } finally {
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        return bool;
    }

    /**
     * 用户注销界面显示用户个人信息
     * @param loginName
     * @return
     */
    @Override
    public List<BorrowerBean> UserInfoService(String loginName) {
        Connection conn = null;
        List<BorrowerBean> a = new ArrayList<>();
        try {
            conn = DBUtil.getConn();
            conn.setAutoCommit(false);
            String sql = "SELECT * FROM borrower WHERE username=?";
            a= borDao.UserInfo(sql,conn,loginName);
            System.out.println(a);
            conn.commit();

        } catch (SQLException e) {
            e.printStackTrace();
            try {
                conn.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }finally {
            if (conn !=null){
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        return a;
    }

    /**
     * 将用户注销状态设置为1
     * @param bor
     * @param loginName
     * @return
     */
    @Override
    public boolean CancelService(BorrowerBean bor, String loginName) {
        boolean bool = false;
        Connection conn = null;
        try {
            conn = DBUtil.getConn();
            conn.setAutoCommit(false);
            String sql = "UPDATE borrower set iscancel=? WHERE username=?";
            bool = borDao.Cancel(sql,conn,bor,loginName);
            if (bool) {
                conn.commit();
            } else {
                conn.rollback();
            }
        } catch (SQLException e){
            e.printStackTrace();
            try {
                if(conn != null)
                    conn.rollback();
            } catch (SQLException throwable) {
                throwable.printStackTrace();
            }

        } finally {
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        return bool;
    }



    /**
     * 提交书籍报损信息
     * @param bor
     * @return
     */
    @Override
    public boolean BookDamageInfoService(BookDamageBean bor) {
        boolean bool = false;
        Connection conn = null;
        try {
            conn = DBUtil.getConn();
            conn.setAutoCommit(false);
            String sql = "INSERT INTO bookdamage (bookdamagemsgid,userid,bookid,bookdamagemsg,damagedegree,checkstatus) VALUES(null,?,?,?,?,'1')";
            bool = borDao.BookDamageInfo(sql, conn, bor);
            if (bool) {
                conn.commit();
            } else {
                conn.rollback();
            }
        } catch (SQLException e){
            e.printStackTrace();
            try {
                if(conn != null)
                    conn.rollback();
            } catch (SQLException throwable) {
                throwable.printStackTrace();
            }

        } finally {
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        return bool;
    }
}
